package com.hlkj.pay.controller.common;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.util.List;
import java.util.Map;

import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.pay.app.common.ICommonAppService;
import com.hlkj.pay.dto.common.*;
import com.hlkj.pay.enums.CommonEnum;
import com.hlkj.pay.infrastructure.model.common.PlatFormConfigDO;
import com.hlkj.pay.service.common.IPlatFormConfigService;
import com.hlkj.pay.vo.common.resp.PlatFormConfigResp;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
/**
 * @author HlpayTeam
 * @date 2024/09/12 15:01
 */
@Tag(name = "基础配置信息")
@RestController
@RequestMapping("/v1/common/config")
@RequiredArgsConstructor
public class ConfigController {
	
	private final IPlatFormConfigService platFormConfigService;
	private final ICommonAppService commonAppService;
	@Operation(summary = "获取短信配置")
	@GetMapping("/sms")
	@ResponseBody
	public CommonResult<SmsConfig> sms() {
		return CommonResult.success(platFormConfigService.getSmsConfig());
	}
	@Operation(summary = "保存短信配置")
	@PostMapping("/sms")
	@ResponseBody
	public CommonResult<Void> smsSave(@Validated @RequestBody SmsConfig smsConfig) {
		PlatFormConfigDO platFormConfigDO=new PlatFormConfigDO();
		platFormConfigDO.setConfigName(CommonEnum.CONFIG_TYPE.SMS.getCode());
		platFormConfigDO.setConfigValue(JsonUtils.toJsonPrettyString(smsConfig));
		commonAppService.saveOrUpdateConfig(platFormConfigDO);
		return CommonResult.success();
	}
	@Operation(summary = "获取存储配置")
	@GetMapping("/storage")
	@ResponseBody
	public CommonResult<StorageConfig> storage() {
		return CommonResult.success(platFormConfigService.getStorageConfig());
	}
	@Operation(summary = "保存存储配置")
	@PostMapping("/storage")
	@ResponseBody
	public CommonResult<Void> storageSave(@Validated @RequestBody StorageConfig storageConfig) {
		PlatFormConfigDO platFormConfigDO=new PlatFormConfigDO();
		platFormConfigDO.setConfigName(CommonEnum.CONFIG_TYPE.STORAGE.getCode());
		platFormConfigDO.setConfigValue(JsonUtils.toJsonPrettyString(storageConfig));
		commonAppService.saveOrUpdateConfig(platFormConfigDO);
		return CommonResult.success();
	}
	@Operation(summary = "获取证照识别配置")
	@GetMapping("/ocr")
	@ResponseBody
	public CommonResult<OcrConfig> ocr() {
		return CommonResult.success(platFormConfigService.getOcrConfig());
	}
	@Operation(summary = "保存证照识别配置")
	@PostMapping("/ocr")
	@ResponseBody
	public CommonResult<Void> ocrSave(@Validated @RequestBody OcrConfig ocrConfig) {
		PlatFormConfigDO platFormConfigDO=new PlatFormConfigDO();
		platFormConfigDO.setConfigName(CommonEnum.CONFIG_TYPE.OCR.getCode());
		platFormConfigDO.setConfigValue(JsonUtils.toJsonPrettyString(ocrConfig));
		commonAppService.saveOrUpdateConfig(platFormConfigDO);
		return CommonResult.success();
	}
	
	@Operation(summary = "获取极验配置")
	@GetMapping("/geetest")
	@ResponseBody
	public CommonResult<GeetestConfig> geetest() {
		return CommonResult.success(platFormConfigService.getGeetestConfig());
	}
	@Operation(summary = "保存极验配置")
	@PostMapping("/geetest")
	@ResponseBody
	public CommonResult<Void> geetestSave(@Validated @RequestBody GeetestConfig geetestConfig) {
		PlatFormConfigDO platFormConfigDO=new PlatFormConfigDO();
		platFormConfigDO.setConfigName(CommonEnum.CONFIG_TYPE.GEETEST.getCode());
		platFormConfigDO.setConfigValue(JsonUtils.toJsonPrettyString(geetestConfig));
		commonAppService.saveOrUpdateConfig(platFormConfigDO);
		return CommonResult.success();
	}
	@Operation(summary = "获取系统配置")
	@GetMapping("/sysConfig")
	@ResponseBody
	public CommonResult<Map<String,Object>> sysConfig() {
		return CommonResult.success(commonAppService.sysConfig());
	}
	@Operation(summary = "获取平台配置")
	@GetMapping("/platFormConfig")
	@ResponseBody
	public CommonResult<PlatFormConfigResp> platFormConfig() {
		List<PlatFormConfigDO> platFormConfigList = platFormConfigService.platFormConfigList(
				CommonEnum.CONFIG_TYPE.APP_INFO.getType());
		if(CollectionUtils.isEmpty(platFormConfigList)){
			return CommonResult.success(null);
		}
		PlatFormConfigResp platFormConfigResp=new PlatFormConfigResp();
		platFormConfigList.stream().forEach(platFormConfig->{
			CommonEnum.CONFIG_TYPE configType = CommonEnum.CONFIG_TYPE.from(platFormConfig.getConfigName());
			if(configType!=null){
				switch (configType){
					case APP_INFO:
						platFormConfigResp.setAppInfoConfig(JsonUtils.parseObject(platFormConfig.getConfigValue(),
								AppInfoConfig.class));
						break;
					case WEB_INFO:
						platFormConfigResp.setWebInfoConfig(JsonUtils.parseObject(platFormConfig.getConfigValue(),
								WebInfoConfig.class));
						break;
					case MERCHANT_INFO:
						platFormConfigResp.setMerchantInfo(JsonUtils.parseObject(platFormConfig.getConfigValue(),
								MerchantInfoConfig.class));
						break;
				}
			}
		});
		return CommonResult.success(platFormConfigResp);
	}
	@Operation(summary = "保存平台配置")
	@PostMapping("/platFormConfig")
	@ResponseBody
	public CommonResult<Void> savePlatFormConfig(@Validated @RequestBody PlatFormConfigResp platFormConfigResp) {
		AppInfoConfig appInfoConfig = platFormConfigResp.getAppInfoConfig();
		WebInfoConfig webInfoConfig = platFormConfigResp.getWebInfoConfig();
		MerchantInfoConfig merchantInfo = platFormConfigResp.getMerchantInfo();
		if(appInfoConfig!=null){
			PlatFormConfigDO platFormConfigDO=new PlatFormConfigDO();
			platFormConfigDO.setConfigName(CommonEnum.CONFIG_TYPE.APP_INFO.getCode());
			platFormConfigDO.setConfigValue(JsonUtils.toJsonPrettyString(appInfoConfig));
			platFormConfigDO.setType(CommonEnum.CONFIG_TYPE.APP_INFO.getType());
			commonAppService.saveOrUpdateConfig(platFormConfigDO);
		}
		if(webInfoConfig!=null){
			PlatFormConfigDO platFormConfigDO=new PlatFormConfigDO();
			platFormConfigDO.setConfigName(CommonEnum.CONFIG_TYPE.WEB_INFO.getCode());
			platFormConfigDO.setConfigValue(JsonUtils.toJsonPrettyString(webInfoConfig));
			platFormConfigDO.setType(CommonEnum.CONFIG_TYPE.WEB_INFO.getType());
			commonAppService.saveOrUpdateConfig(platFormConfigDO);
		}
		if(merchantInfo!=null){
			PlatFormConfigDO platFormConfigDO=new PlatFormConfigDO();
			platFormConfigDO.setConfigName(CommonEnum.CONFIG_TYPE.MERCHANT_INFO.getCode());
			platFormConfigDO.setConfigValue(JsonUtils.toJsonPrettyString(merchantInfo));
			platFormConfigDO.setType(CommonEnum.CONFIG_TYPE.MERCHANT_INFO.getType());
			commonAppService.saveOrUpdateConfig(platFormConfigDO);
		}
		return CommonResult.success();
	}
	@Operation(summary = "获取默认配置")
	@GetMapping("/defaultConfig")
	@ResponseBody
	public CommonResult<DefaultConfig> defaultConfig() {
		return CommonResult.success(platFormConfigService.getDefaultConfig());
	}
	@Operation(summary = "保存默认配置")
	@PostMapping("/defaultConfig")
	@ResponseBody
	public CommonResult<Void> saveDefaultConfig(@Validated @RequestBody DefaultConfig defaultConfig) {
		PlatFormConfigDO platFormConfigDO=new PlatFormConfigDO();
		platFormConfigDO.setConfigName(CommonEnum.CONFIG_TYPE.DEFAULT.getCode());
		platFormConfigDO.setConfigValue(JsonUtils.toJsonPrettyString(defaultConfig));
		platFormConfigDO.setType(CommonEnum.CONFIG_TYPE.DEFAULT.getType());
		commonAppService.saveOrUpdateConfig(platFormConfigDO);
		return CommonResult.success();
	}
	@Operation(summary = "获取RSA配置")
	@GetMapping("/rsaConfig")
	@ResponseBody
	public CommonResult<RsaConfig> rsaConfig() {
		return CommonResult.success(platFormConfigService.getRsaConfig());
	}
	@Operation(summary = "保存RSA配置")
	@PostMapping("/rsaConfig")
	@ResponseBody
	public CommonResult<Void> saveRsaConfig(@Validated @RequestBody RsaConfig rsaConfig) {
		PlatFormConfigDO platFormConfigDO=new PlatFormConfigDO();
		platFormConfigDO.setConfigName(CommonEnum.CONFIG_TYPE.RSA_CONFIG.getCode());
		platFormConfigDO.setConfigValue(JsonUtils.toJsonPrettyString(rsaConfig));
		platFormConfigDO.setType(CommonEnum.CONFIG_TYPE.RSA_CONFIG.getType());
		commonAppService.saveOrUpdateConfig(platFormConfigDO);
		return CommonResult.success();
	}
	@Operation(summary = "获取政策协议")
	@GetMapping("/agreement")
	@ResponseBody
	public CommonResult<AgreementConfig> agreement() {
		List<PlatFormConfigDO> platFormConfigList = platFormConfigService.platFormConfigList(
				CommonEnum.CONFIG_TYPE.AGREEMENT.getType());
		if(CollectionUtils.isEmpty(platFormConfigList)){
			return CommonResult.success(null);
		}
		PlatFormConfigDO platFormConfigDO = platFormConfigList.get(0);
		return CommonResult.success(JsonUtils.parseObject(platFormConfigDO.getConfigValue(),AgreementConfig.class));
	}
	@Operation(summary = "保存政策协议")
	@PostMapping("/agreement")
	@ResponseBody
	public CommonResult<Void> saveAgreementConfig(@Validated @RequestBody AgreementConfig agreementConfig) {
		PlatFormConfigDO platFormConfigDO=new PlatFormConfigDO();
		platFormConfigDO.setConfigName(CommonEnum.CONFIG_TYPE.AGREEMENT.getCode());
		platFormConfigDO.setConfigValue(JsonUtils.toJsonPrettyString(agreementConfig));
		platFormConfigDO.setType(CommonEnum.CONFIG_TYPE.AGREEMENT.getType());
		commonAppService.saveOrUpdateConfig(platFormConfigDO);
		return CommonResult.success();
	}

	@Operation(summary = "首页无token获取政策协议")
	@GetMapping("/getAgreement")
	@ResponseBody
	public CommonResult<AgreementConfig> getAgreement() {
		List<PlatFormConfigDO> platFormConfigList = platFormConfigService.platFormConfigList(
				CommonEnum.CONFIG_TYPE.AGREEMENT.getType());
		if(CollectionUtils.isEmpty(platFormConfigList)){
			return CommonResult.success(null);
		}
		PlatFormConfigDO platFormConfigDO = platFormConfigList.get(0);
		return CommonResult.success(JsonUtils.parseObject(platFormConfigDO.getConfigValue(),AgreementConfig.class));
	}
	
}
